package com.example.labb2da523b;

import java.sql.SQLException;

import com.example.labb2da523b.db.SqlHelper;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BudgetActivity extends Activity {
	
	private Button saveButton;
	private EditText budgetAmount;
	private int monthlyBudget;
	SqlHelper db = new SqlHelper(this);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_budget);
		ActionBar ab = getActionBar();
		ab.setDisplayShowCustomEnabled(true);
		ab.setDisplayShowTitleEnabled(false);
		ab.setCustomView(R.layout.title_theme);
		saveButton = (Button) findViewById(R.id.budgetsaves);
		budgetAmount = (EditText) findViewById(R.id.budgettext);
		try {
			db.open();
			budgetAmount.setText(""+db.getBudget(), EditText.BufferType.EDITABLE);
			db.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
			db.close();
		}
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				monthlyBudget = Integer.parseInt(budgetAmount.getText().toString());
				try {
					db.open();
					db.insertBudget(monthlyBudget);
					db.close();
					Toast.makeText(BudgetActivity.this, "New Monthly budget saved", Toast.LENGTH_LONG).show();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(BudgetActivity.this, "Problem occured, budget was not saved", Toast.LENGTH_LONG).show();
					db.close();
				}
				monthlyBudget = 0;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuHelper.Menu(item, this);
	}
}